<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<t:base type="jquery,easyui,tools,DatePicker"></t:base>
<t:datagrid name="newstemplatelist" checkbox="true"
	actionUrl="wxNewsTemplateController.do?datagrid" fit="true"
	fitColumns="true" idField="id" queryMode="group" title="图文消息">
	<t:dgCol title="编号" field="id" hidden="true"></t:dgCol>
	<t:dgCol title="图文名称" field="templateName" query="true" width="100"></t:dgCol>
	<t:dgCol title="创建日期" field="createDate" formatter="yyyy-MM-dd"
		width="90"></t:dgCol>
	<t:dgCol title="是否上传微信" field="mediaId" queryMode="single" width="120"
		formatterjs="fmtMediaId"></t:dgCol>
	<t:dgCol title="操作" field="opt"></t:dgCol>
	<t:dgDelOpt title="删除" url="wxNewsTemplateController.do?del&id={id}" />
	<t:dgFunOpt funname="addItem(id)" title="添加图文"></t:dgFunOpt>
	<t:dgFunOpt funname="readNews(id)" title="查看图文"></t:dgFunOpt>
	<t:dgFunOpt title="上传" exp="mediaId#empty#true" funname="uploadOne(id)"></t:dgFunOpt>
	<t:dgToolBar title="图文录入" icon="icon-add"
		url="wxNewsTemplateController.do?goEditView" funname="add"></t:dgToolBar>
	<t:dgToolBar title="图文编辑" icon="icon-edit"
		url="wxNewsTemplateController.do?goEditView" funname="update"></t:dgToolBar>
	<t:dgToolBar title="批量删除" icon="icon-remove"
		url="wxNewsTemplateController.do?doBatchDel" funname="deleteALLSelect"></t:dgToolBar>
	<t:dgToolBar title="批量上传" icon="icon-remove"
		url="wxNewsTemplateController.do?doBatchUpload"
		funname="uploadAllSelect"></t:dgToolBar>
</t:datagrid>
<script type="text/javascript">
  function fmtMediaId(value, row, index) {
    return value ? "已上传" : "<font color='red'>未上传</font>";
  }

  function readNews(id) {
    createdetailwindow('图文编辑', 'wxArticleController.do?goMessage&templateId='
        + id, 'newstemplatelist', '100%', '100%');
  }

  function addItem(id) {
    add('添加图文', 'wxArticleController.do?goAdd&templateId=' + id,
        'newstemplatelist', '100%', '100%');
  }

  function uploadOne(id) {
    $.dialog.confirm('你确定上传该数据吗?', function(r) {
      if (r) {
        var ids = [];
        ids.push(id);
        uploadAll(ids);
      }
    });
  }

  function uploadAllSelect(title, url, gname) {
    gridname = gname;
    var ids = [];
    var rows = $("#" + gname).datagrid('getSelections');
    for (var i = 0; i < rows.length; i++) {
      ids.push(rows[i].id);
    }
    if (ids.length <= 0) {
      tip("请选择需要上传的数据");
      return;
    }

    $.dialog.setting.zIndex = getzIndex(true);
    $.dialog.confirm('你确定上传该图文吗?', function(r) {
      if (r) {
        uploadAll(ids);
      }
    });
  }

  function uploadAll(ids) {
    $.ajax({
      url : "wxNewsTemplateController.do?doBatchUpload",
      type : 'post',
      data : {
        ids : ids.join(',')
      },
      cache : false,
      success : function(data) {
        var d = $.parseJSON(data);
        if (d.success) {
          var msg = d.msg;
          tip(msg);
          reloadTable();
          $("#" + gname).datagrid('unselectAll');
          ids = '';
        } else {
          tip("上传失败: " + d.msg);
        }
      }
    });
  }
</script>

